package tpo.wmb.provider.dao;

import dto.ShowWares;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.DetailList;

import java.util.List;
import java.util.Map;

@Repository
public interface DetailListMapper {

    /**
     * 根据订单号联合查询商品到 ShowWares 清单对象
     */
    @Select("SELECT detail_list.id sum , detail_list.`total`, detail_list.`quantity`, detail_list.`remark` brand,detail_list.returnNum ksum,  wares.`log`, wares.id, wares.`name`, wares.`bPrice`, unit.`name` unit, house_wa.`kStock`\n" +
            "FROM `detail_list`\n" +
            "INNER JOIN `wares`\n" +
            "ON `detail_list`.`wa_spId` = wares.`id`\n" +
            "INNER JOIN `unit`\n" +
            "ON unit.`id` = wares.unitId\n" +
            "INNER JOIN house_wa\n" +
            "ON `house_wa`.waresId = wares.`id`\n" +
            "INNER JOIN \n" +
            "(SELECT defaultWarehouse FROM `user`\n" +
            "INNER JOIN buy_order\n" +
            "ON `user`.`id` = buy_order.`userId`\n" +
            "WHERE buy_order.code = #{orderId}) a\n" +
            "ON `house_wa`.warehouseId = a.defaultWarehouse\n" +
            "WHERE orderId = #{orderId} AND type = #{type}")
    List<ShowWares> selectDetailList(String orderId, Integer type );


    /**
     * 根据退货单号联合查询商品到 ShowWares 清单对象
     * @param orderId
     * @param type
     * @return
     */
    @Select("SELECT detail_list.id `sum` , detail_list.`total`, detail_list.`quantity`, detail_list.`remark` brand,detail_list.returnNum ksum,  wares.`log`, wares.id, wares.`name`, wares.`bPrice`, unit.`name` unit, house_wa.`kStock`\n" +
            "FROM `detail_list`\n" +
            "INNER JOIN `wares`\n" +
            "ON `detail_list`.`wa_spId` = wares.`id`\n" +
            "INNER JOIN `unit`\n" +
            "ON unit.`id` = wares.unitId\n" +
            "INNER JOIN house_wa\n" +
            "ON `house_wa`.waresId = wares.`id`\n" +
            "INNER JOIN\n" +
            "(SELECT defaultWarehouse FROM `user`\n" +
            "INNER JOIN `buy_return`\n" +
            "ON `user`.`id` = `buy_return`.`userId`\n" +
            "WHERE `buy_return`.code = #{orderId}) a\n" +
            "ON `house_wa`.warehouseId = a.defaultWarehouse\n" +
            "WHERE orderId =#{orderId}  AND `type` = #{type}")
    List<ShowWares> selectDetailList2( String orderId, Integer type );


    /**
     * 新增明细表
     * @param detailList
     * @return
     */
    @Insert("insert into detail_list (type, orderId, wa_spId, quantity, total, remark) values(#{type}, #{orderId}, #{wa_spId}, #{quantity}, #{total}, #{remark})")
    Integer insertDetailList( DetailList detailList);


    /**
     *修改明细表
     * @param detailList
     * @return
     */
//    @Update("update detail_list set type = #{type}, orderId = #{orderId}, wa_spId = #{wa_spId}, quantity = #{quantity}, total = #{total}, remark = #{remark}, returnNum = #{returnNum} where id = #{id}")
    Integer updateDetailList( DetailList detailList);


    /**
     * 删除明细表
     * @param id
     * @return
     */
    @Delete("delete from detail_list where id = #{id}")
    Integer deleteDetailList( Integer id);

    /**
     * 删除一个订单的所有明细表
     * @param code
     * @return
     */
    @Delete("delete from detail_list where orderId = #{code}")
    Integer deleteDetailList2( String code);


    /**
     * 订单明细和商品联合查询
     * @param orderId
     * @return
     */
    Map<String,Object> detailListWares(String orderId);

}
